import 'package:flutter/material.dart';
import 'package:mqshop/services/cart_service.dart';
import 'package:provider/provider.dart';
import 'package:fluttertoast/fluttertoast.dart';

import '../../services/screen_adapter.dart';
import '../../provider/cart.dart';
import '../../provider/checkout.dart';
import '../cart/cart_item.dart';
import '../../services/user_service.dart';

class CartPage extends StatefulWidget {
  CartPage({Key key}) : super(key: key);

  @override
  _CartPageState createState() => _CartPageState();
}

class _CartPageState extends State<CartPage> {

  bool _isEdit = false;


  /// 结算操作
  doCheckout() async{
    /// 获取购物车选中的数据
    List checkoutData = await CartService.getCheckoutData();

    /// 判断购物车有么有选中的数据
    if(checkoutData.isNotEmpty) {
      /// 保存购物车选中的数据
      context.read<CheckOut>().changeCheckOutList(checkoutData);

      /// 判断用户有没有登录
      bool loginState = await UserService.getUserLoginState();
      if(loginState) {
        Navigator.pushNamed(context, '/checkout');
      } else {
        Fluttertoast.showToast(
          msg: "请先登录再结算",
          toastLength: Toast.LENGTH_SHORT,
          gravity: ToastGravity.CENTER,
          timeInSecForIosWeb: 1,
          backgroundColor: Colors.grey[600],
          textColor: Colors.white,
          fontSize: 16.0
        );
        Navigator.pushNamed(context, '/login');
      }

    } else {
      Fluttertoast.showToast(
        msg: "购物车没有选中的数据",
        toastLength: Toast.LENGTH_SHORT,
        gravity: ToastGravity.CENTER,
        timeInSecForIosWeb: 1,
        backgroundColor: Colors.grey[600],
        textColor: Colors.white,
        fontSize: 16.0
      );
    }
       
    
  }

  @override
  Widget build(BuildContext context) {

    ScreenAdapter.init();
    var cartProvider = context.watch<Cart>();

    return Scaffold(
      appBar: AppBar(
        title: Text('购物车'),
        actions: <Widget>[
          IconButton(
            icon: !_isEdit ? Text('编辑') : Text('完成'), 
            onPressed: () {
              setState(() => _isEdit = !_isEdit);
            }
          )
        ]
      ),
      body: cartProvider.cartList.isNotEmpty ? Stack(
        children: <Widget>[
           
          ListView(
            children: <Widget>[
              Column(
                children: cartProvider.cartList.map((value) {
                  return CartItem(value);
                }).toList()
              ),
              SizedBox(height: ScreenAdapter.height(100))
            ]  
          ),

          Positioned(
            bottom: 0,
            width: ScreenAdapter.width(750),
            height: ScreenAdapter.height(78),
            child: Container(
              width: ScreenAdapter.width(750),
              height: ScreenAdapter.height(78),
              decoration: BoxDecoration(
                color: Colors.white,
                border: Border(
                  top: BorderSide(
                    width: 1,
                    color: Colors.black12
                  )
                )
              ),
              child: Stack(
                children: <Widget>[
                  Align(
                    alignment: Alignment.centerLeft,
                    child: Row(
                      children: <Widget>[
                        Container(
                          width: ScreenAdapter.width(60),
                          child: Checkbox(
                            value: cartProvider.isCheckedAll, 
                            onChanged: (value) {
                              /// 实现全选或反选
                              context.read<Cart>().checkAll(value);
                            },
                            activeColor: Colors.pink
                          )
                        ),
                        Text('全选'),
                        SizedBox(width: 20),
                        !_isEdit ? Text('合计:', style: TextStyle(
                          fontSize: 20
                        )) : Text(''),
                        !_isEdit ? Text('¥${cartProvider.allPrice}', style: TextStyle(
                          fontSize: 20,
                          color: Colors.red
                        )) : Text('')
                      ]
                    )
                  ),
                  !_isEdit ? Align(
                    alignment: Alignment.centerRight,
                    child: RaisedButton(
                      onPressed: doCheckout,
                      child: Text('结算', style: TextStyle(color: Colors.white)),
                      color: Colors.red
                    )
                  ) : Align(
                    alignment: Alignment.centerRight,
                    child: RaisedButton(
                      onPressed: () {
                        context.read<Cart>().removeItem();
                      },
                      child: Text('删除', style: TextStyle(color: Colors.white)),
                      color: Colors.red
                    )
                  )
                ]
              )
            )
          )

        ]
      ) : Center(
        child: Text('购物车是空的')
      )
    );
  }
}